package org.gongliang.jsmile.gen.db;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 *
 * @Description 数据库连接工具类
 * @author 龚亮
 * @date 2017年3月24日 下午5:23:31
 */
public class DBUtils {
	private static String driver;
	private static String url;
	private static String username;
	private static String password;

	public static String basePackname;
	public static String mybatisPath;
	public static String srcPath;
	public static String[] removedTableNamePrefixes;
	public static String[] tableNames;

	public static void main(String[] args) {
		System.out.println(getTableNames(getConnection())[0]);
	}

	static {
		try {
			ResourceBundle bundle = ResourceBundle.getBundle("db");
			username = bundle.getString("db.master.username");
			password = bundle.getString("db.master.password");
			url = bundle.getString("db.master.url");
			driver = bundle.getString("db.master.driver");

			basePackname = bundle.getString("basePackname");
			mybatisPath = bundle.getString("mybatisPath");
			removedTableNamePrefixes = bundle.getString("removedTableNamePrefixes").split(",");
			tableNames = bundle.getString("tableNames").split(",");
			srcPath=bundle.getString("srcPath");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取数据库连接
	 */
	public static Connection getConnection() {
		Connection con = null;
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	/**
	 * 关闭数据库连接
	 */
	public static void close(ResultSet rs, PreparedStatement ps, Connection con) {
		try {
			if (rs != null)
				rs.close();
			if (ps != null)
				ps.close();
			if (con != null)
				con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取所有数据库表名
	 */
	public static String[] getTableNames(Connection conn) {
		String[] tableNames = new String[] {};
		try {
			DatabaseMetaData meta = conn.getMetaData();
			ResultSet rs = meta.getTables(null, null, null, new String[] { "TABLE" });
			rs.last();
			tableNames = new String[rs.getRow()];
			int i = 0;
			rs.beforeFirst();
			while (rs.next()) {
				tableNames[i] = rs.getString(3);
				i++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tableNames;
	}

	public static Map<String, Object> getEntity(String sql, Connection conn) {
		Map<String, Object> entity = new HashMap<String, Object>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			int column = rsmd.getColumnCount();
			while (rs.next()) {
				Object[] objects = new Object[column];
				Map<String, Object> map = new HashMap<String, Object>();
				for (int i = 0; i < objects.length; i++) {
					Object value = rs.getObject(i + 1);
					String key = rsmd.getColumnName(i + 1);
					map.put(key, value);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return entity;
	}

	public static void close(Connection conn) {
		try {
			if (conn != null)
				conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}